package com.ctz.commonframe.ui;

import java.util.ArrayList;
import java.util.List;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import com.ctz.commonframe.constant.DeviceInfo;
import com.ctz.commonframe.ui.view.LoadingDialog;
import com.ctz.commonframe.utils.CommonUtil;

import org.xutils.x;

public class BaseActivity extends FragmentActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
	private static List<BaseActivity> activityList = new ArrayList<>();
    private LoadingDialog loadingDialog;

	@TargetApi(Build.VERSION_CODES.KITKAT)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
//        overridePendingTransition(R.anim.push_right_in, 0);//进入的动画
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		activityList.add(this);
		super.onCreate(savedInstanceState);
		x.view().inject(this);
		Window window = getWindow();
		//沉浸式标题栏
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			WindowManager.LayoutParams localLayoutParams = window.getAttributes();
			localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
		}

//		// 使软键盘默认隐藏
//		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

		if (DeviceInfo.screenWidth == 0 || DeviceInfo.screenHeight == 0 || DeviceInfo.screenDensity == 0) {
			DisplayMetrics displayMetrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
			DeviceInfo.screenWidth = displayMetrics.widthPixels;
			DeviceInfo.screenHeight = displayMetrics.heightPixels;
			DeviceInfo.screenDensity = displayMetrics.density;
		}
		if (DeviceInfo.imei == null){
			DeviceInfo.imei = CommonUtil.getIMEI(this);
		}
		if (DeviceInfo.model == null){
			DeviceInfo.model = android.os.Build.MODEL + android.os.Build.BRAND;
		}
	}

	@Override
	protected void onDestroy() {
		activityList.remove(this);
		super.onDestroy();
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		switch (requestCode) {
			case CommonUtil.REQUEST_CODE_READ_PHONE_STATE:
				if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    DeviceInfo.imei = CommonUtil.getIMEI(BaseActivity.this);
				}
				break;

			default:
				break;
		}
	}

	/**
	 * 退出应用
	 */
	public void exit() {
		try {
			for (BaseActivity activity : activityList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 短时间显示Toast
	 * @param content 内容
	 */
	public void showToast(String content){
		if (!TextUtils.isEmpty(content)) {
			Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
		}
	}
	
	/**
	 * 短时间显示Toast
	 * @param stringId 字符串资源id
	 */
	public void showToast(int stringId){
		String text = BaseActivity.this.getResources().getString(stringId);
		showToast(text);
	}
	
	/**
	 * 长时间显示Toast
	 * @param text 内容
	 */
	public void showLongToast(String text){
		Toast.makeText(BaseActivity.this, text, Toast.LENGTH_LONG).show();
	}
	
	/**
	 * 长时间显示Toast
	 * @param stringId 字符串资源id
	 */
	public void showLongToast(int stringId){
		String text = BaseActivity.this.getResources().getString(stringId);
		showLongToast(text);
	}
	
	/**
	 * 隐藏软键盘
	 * @param view 控件
	 */
	public void hideKeyboard(View view){
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
		    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}

	public void showLoadingDialog() {
		if (loadingDialog == null)
			loadingDialog = new LoadingDialog(BaseActivity.this);
		loadingDialog.show();
	}

	public void dismissLoadingDialog() {
		if(loadingDialog != null){
			loadingDialog.dismiss();
		}
	}
}